package com.salt.security;


import com.google.common.collect.Lists;
import com.salt.common.BusinessException;
import com.salt.common.ResponseStatus;
import com.salt.system.mapper.SysAuthoritiesMapper;
import com.salt.system.mapper.SysUserAuthoritiesMapper;
import com.salt.system.mapper.SysUserMapper;
import com.salt.system.model.domain.SysAuthorities;
import com.salt.system.model.domain.SysUser;
import com.salt.system.model.domain.SysUserAuthorities;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户信息实现
 *
 * @author HaiBo Kuang
 * @date 2021年04月16日 下午 13:44
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    private final SysUserMapper userMapper;
    private final SysAuthoritiesMapper sysAuthoritiesMapper;
    private final SysUserAuthoritiesMapper sysUserAuthoritiesMapper;

    @Autowired
    public UserDetailsServiceImpl(SysUserMapper userMapper,
                                  SysAuthoritiesMapper sysAuthoritiesMapper,
                                  SysUserAuthoritiesMapper sysUserAuthoritiesMapper) {
        this.userMapper = userMapper;
        this.sysAuthoritiesMapper = sysAuthoritiesMapper;
        this.sysUserAuthoritiesMapper = sysUserAuthoritiesMapper;
    }


    /**
     * 通过用户名 加载用户
     *
     * @param username 用户名
     * @return UserDetails
     * @throws UsernameNotFoundException 用户名未找到异常
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser query = new SysUser();
        query.setUserName(username);
        SysUser sysUser = userMapper.selectOne(query);
        // 自定义通过用户名(或电话)加载用户
        if (Objects.isNull(sysUser)) {
            query.setUserName(null);
            query.setPhone(username);
            sysUser = userMapper.selectOne(query);
        }
        if (Objects.isNull(sysUser)) {
            throw new BusinessException(ResponseStatus.USER_NOT_FOUND);
        }
        // 查询用户权限
        SysUserAuthorities userAuthoritiesQuery = new SysUserAuthorities();
        userAuthoritiesQuery.setUserId(sysUser.getId());
        List<SysUserAuthorities> sysUserAuthorities = sysUserAuthoritiesMapper.select(userAuthoritiesQuery);
        List<Long> authoritiesIds = sysUserAuthorities.stream().
                map(SysUserAuthorities::getAuthoritiesId).collect(Collectors.toList());
        if (authoritiesIds.isEmpty()) {
            // 用户没有权限，实际上也是可登录的
            return new BaseUserDetails(sysUser, Lists.newArrayList());
        }
        List<SysAuthorities> authorities = sysAuthoritiesMapper.findAllByIdIn(authoritiesIds);
        return new BaseUserDetails(sysUser, authorities);
    }
}
